<?php
/**
 * Creater: 李青林
 * Date Time : 2018/10/11 10:56
 */

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\Goods;
use App\Http\Models\User;
use App\Http\Models\GoodsCopywritingError;
use Auth;
use DB;
//use Illuminate\Http\Request;
use function Sodium\crypto_box_publickey_from_secretkey;
use Illuminate\Support\MessageBag;
use App\Admin\Extensions\CheckRowEdit;
use Encore\Admin\Widgets\Table;

class GoodsCopywritingController extends Controller
{
    use ModelForm;
    protected $headerStr = '未入库文案';

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '');
            $content->body($this->grid());
        });

    }

    protected function form()
    {
        return Admin::form(GoodsCopywritingError::class, function (Form $form) {
            $form->hidden('id');
            $form->text('group_num', 'QQ群:');
            $form->text('goodstitle', '文案标题:');
            $form->text('couponlink', '优惠券链接:');
            $form->text('goodslink', '商品链接:');
            $form->currency('origprice', '原价:')->symbol('￥');
            $form->currency('voucherprice', '券后价:')->symbol('￥');
            $form->textarea('copywriting', '采集失败文案:')->rows(10);
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                // $tools->disableListButton(); // 去掉跳转列表按钮
                // $tools->disableDelete(); // 去掉跳转列表按钮
                //$tools->disableView(); // 去掉跳转列表按钮
                if (method_exists('tools', 'disableReset')) {
                    $tools->disableReset(); // 去掉重置按钮
                }
            });
            $form->saved(function (Form $form) {
                if ($form->goodslink && $form->couponlink && $form->origprice && $form->voucherprice) {
                    $goods_copywriting = GoodsCopywritingError::find($form->id);
                    $goods_copywriting->update_flag = 1;
                    $goods_copywriting->save();
                }
            });
        });
    }

    protected function grid()
    {
        return Admin::grid(GoodsCopywritingError::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('编号')->sortable();
            $grid->goodstitle('商品信息')->display(function () {
                if ($this->goodslink && $this->goodstitle && $this->origprice && $this->voucherprice) {
                    $info =
                        [
                            '商品标题' => '<a target="_blank" href="' . $this->goodslink . '">' . str_limit($this->goodstitle, $limit = 30, $end = '...') . '</a>',
                            '原价/券后价' => '￥' . $this->origprice . ' / ￥' . $this->voucherprice,
                            '优惠券信息' => $this->couponlink ? '<a target="_blank" href="' . $this->couponlink . '">￥ ' . ($this->origprice - $this->voucherprice) . '</a>' : '',
                        ];
                } else {
                    $info =
                        [
                            '商品标题' => '',
                            '原价/券后价' => '',
                            '优惠券信息' => '',
                        ];
                }
                return new Table([], $info);
            });
            $grid->group_num('QQ群');
            $grid->update_flag('状态')->display(function ($value) {
                return show_goods_writing_style($value);
            });
            $grid->created_at('提交时间');
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->paginate(env('Admin_Limit'));
            $grid->disableCreateButton();
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                $filter->equal('group_num', 'QQ群');
                $filter->like('goodstitle', '商品标题');
                $filter->equal('update_flag', '状态')->select([
                    '0' => '未修改',
                    '1' => '已修改',
                    '2' => '采集器已更新',
                ]);
            });
            if (Admin::user()->isRole('administrator') || Admin::user()->isRole('operator')) {
                $grid->actions(function ($actions) {
                    $actions->disableDelete();
                    if (method_exists($actions, 'disableView')) {
                        $actions->disableView();
                    }
                });

            } else {
                $grid->disableActions();
            }

        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form());
        });
    }

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }
}